<?php
class iringback{
//Check format of phone number
	static public function checkPhoneNumber($phoneNumber)
	{	$result = new stdClass();
		$result->success = false;
		$result->error_code = 0;
		$result->phone = "";		
		$phoneNumber = trim($phoneNumber);
		$length 	= strlen($phoneNumber);
		if ($length > 0)
		{
			if (( $length < 10 ) || ( $length > 12 )) {
				$result->message = sfConfig::get('phone_wrong_num_chars');//"Số điện thoại phải có từ 10 đến 13 chữ số";
				$result->error_code = 7;
			}else 
			{
				for ( $i =0; $i < $length; $i++)
				{
					if (!is_numeric($phoneNumber[$i])) {
						$result->success = false;
						$result->error_code = 8;
						$result->message = sfConfig::get('phone_must_be_num');//"Số điện thoại chỉ được là các chữ số";
						break;
					}
				}
				if ($i == $length)
				{	
					if($phoneNumber[0] == '0')
					{
						$result->phone = "84".substr($phoneNumber,1,$length-1);
						$result->phone2 = substr($phoneNumber,1,$length-1);
						$result->success = true;
					}
					else if (($phoneNumber[0] == '8') && ($phoneNumber[1] == '4') && ($phoneNumber[2] != '0'))
					{						
						$result->success = true;
						$result->phone = $phoneNumber;
						$result->phone2 = substr($phoneNumber,2,$length-2);
					}
					else if (($phoneNumber[0] == '8') && ($phoneNumber[1] == '4') && ($phoneNumber[2] == '0'))
					{
						$result->phone = "84".substr($phoneNumber,3,$length-3);
						$result->phone2 = substr($phoneNumber,3,$length-3);
						$result->success = true;			
					}
					else
					{
						$result->success = false;
						$result->message = sfConfig::get('phone_not_begin_84');//"Số điện thoại phải bắt đầu với 84... hoặc 0...";
						$result->error_code = 9;
					}
				}
			}
		}else
		{
			$result->success = false;
		    $result->message = sfConfig::get('phone_not_empty');//"Số điện thoại bị bỏ trống";
		    $result->error_code = 10;	
		}
		return $result;
	}
	
	static public function GetFLVDuration($file){
      // get contents of a file into a string
      if (file_exists($file)){
            $handle = fopen($file, "r");
            $contents = fread($handle, filesize($file));
            fclose($handle);
            
            if (strlen($contents) > 3){
                  if (substr($contents,0,3) == "FLV"){
                        $taglen = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
                        if (strlen($contents) > $taglen){
                              $duration = hexdec(bin2hex(substr($contents,strlen($contents)-$taglen,3)));
                              return $duration;
                        }
                  }
            }
      }
      return false;
	}
	
	static public function strKhongDau($str)
	{
		$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
	                "ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
	                ,"ế","ệ","ể","ễ",
	                "ì","í","ị","ỉ","ĩ",
	                "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
	                ,"ờ","ớ","ợ","ở","ỡ",
	                "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
	                "ỳ","ý","ỵ","ỷ","ỹ",
	                "đ",
	                "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
	                ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
	                "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
	                "Ì","Í","Ị","Ỉ","Ĩ",
	                "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
	                ,"Ờ","Ớ","Ợ","Ở","Ỡ",
	                "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
	                "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
	                "Đ","?");
	
	    $marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
	                    ,"a","a","a","a","a","a",
	                    "e","e","e","e","e","e","e","e","e","e","e",
	                    "i","i","i","i","i",
	                    "o","o","o","o","o","o","o","o","o","o","o","o"
	                    ,"o","o","o","o","o",
	                    "u","u","u","u","u","u","u","u","u","u","u",
	                    "y","y","y","y","y",
	                    "d",
	                    "A","A","A","A","A","A","A","A","A","A","A","A"
	                    ,"A","A","A","A","A",
	                    "E","E","E","E","E","E","E","E","E","E","E",
	                    "I","I","I","I","I",
	                    "O","O","O","O","O","O","O","O","O","O","O","O"
	                    ,"O","O","O","O","O",
	                    "U","U","U","U","U","U","U","U","U","U","U",
	                    "Y","Y","Y","Y","Y",
	                    "D","");
	                    
	    $str = str_replace($marTViet,$marKoDau,$str);
	    return $str;
	}
	
	static public function getPathStorage($rootDir, $itemId, $itemPerDir, $makeDir)
	{			
		$dir1 = floor($itemId / ($itemPerDir * $itemPerDir * $itemPerDir));
		$dir2 = floor(($itemId - $dir1 * $itemPerDir * $itemPerDir * $itemPerDir)/($itemPerDir * $itemPerDir));
		$temp = floor($itemId / ($itemPerDir * $itemPerDir));
		$dir3 = floor(($itemId -  $temp * $itemPerDir * $itemPerDir)/$itemPerDir);
			
		$path = $dir1 . "/" . $dir2 . "/" .$dir3;
		
		if (!file_exists($rootDir. "/" . $path) && ($makeDir))
		{
			if (!file_exists($rootDir. "/" . $dir1 . "/" . $dir2))
			{
				if (!file_exists($rootDir. "/" . $dir1))
				{
					mkdir($rootDir. "/" . $dir1, true);
				}
				mkdir($rootDir. "/" . $dir1 . "/" . $dir2, true);
				chmod($rootDir. "/" . $dir1, 0777 );
			}
			mkdir($rootDir. "/" . $dir1 . "/" . $dir2, true);
			chmod($rootDir. "/" . $dir1 . "/" . $dir2, 0777 );
		}
		mkdir($rootDir. "/" . $dir1 . "/" . $dir2 . "/" . $dir3, true);
		chmod($rootDir. "/" . $dir1 . "/" . $dir2 . "/" . $dir3, 0777 );
		return $path;
	}
	
}
?>